<?php
include_once('config/config.php');

// 判断是否登录函数
$LoginAdmin = checkAuth();

// 判断是否有post请求
if ($_GET) {
    // 接收参数
    $depid = trim($_GET['depid']) ?? '';
    $jobname = trim($_GET['job']) ?? '';
    // echo $depid;
    // echo $jobname;
    // exit;

    // 判断是否传参
    if (!$depid || !$jobname) {
        showMsg('参数错误', 'jobList.php');
        exit;
    }
    // 查询出部门id函数
    $depSql = "SELECT `id` FROM pre_department WHERE `id` = '$depid'";
    $depData = find($depSql);
    // 判断部门是否存在
    if (!$depData) {
        showMsg('部门不存在', 'jobList.php');
        exit;
    }

    // 封装数据
    $data = [
        'name' => $jobname,
        'depid' => $depData['id']
    ];

    // 插入数据函数
    $result = add('job', $data);
    if ($result['code'] === 1) {
        showMsg('添加成功', 'jobList.php');
    } else {
        echo $result['data'];
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>
<style>

</style>

<body>

    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加职位</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="jobList.php">jobList</a> <span class="divider">/</span></li>
            <li class="active">jobAdd</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">

                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i> 职位列表</button>
                    <div class="btn-group">
                    </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home">
                            <form method="post">
                                <label>部门</label>
                                <select name="depid" id="depid" class="input-xlarge">
                                    <option value="0">请选择</option>

                                </select>

                                <label>职位</label>
                                <input type="text" name="job" id="job" class="input-xlarge">

                                <label></label>
                                <input class="btn btn-primary" id="sub" type="submit" value="提交" />
                            </form>
                        </div>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>

            </div>
        </div>
    </div>

</body>

</html>

<?php include_once('common/script.php'); ?>

<script>
    // 查询部门
    (function() {
        $.ajax({
            type: 'post',
            url: 'presonAdd.php',
            data: {
                action: 'depid'
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 1) {

                    let temp = '';

                    for (let item of res.data) {
                        temp += `<option value="${item.id}">${item.name}</option>`
                    }

                    $('#depid').append(temp);

                    let depid = res.data[0].id ? res.data[0].id : '';

                }
            }
        });
    })();

    $('#sub').click(function(e) {
        e.preventDefault();
        let depid = $('#depid').val();
        if(depid === '0'){
            alert('请选择部门');
            return;
        }
        let job = $('#job').val();
        location.href = 'jobAdd.php?depid=' + depid + '&job=' + job;
        //    console.log(depid,jobid);
    });



    // let $depid = '';
    // // 选择部门时更新
    // $('#depid').change(function() {
    //     let depid = $(this).val();
    // });


    // $('#job').change(function(){
    //     let jobid = $(this).val();
    //     $.ajax({
    //       type:'post',
    //       url:'presonAdd.php',
    //       data:{
    //         action:'job',
    //         jobid,
    //         depid,

    //       },
    //       dataType:'json',
    //       success:function(res){

    //       }
    //     })
    // }); 
</script>